package frontend.model.admin;

import backend.model.admin.Employee;
import backend.model.worklog.WorkLog;
import backend.service.worklog.WorkLogService;
import frontend.core.TimeTrackingWebApplication;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class CalendarDayUIModel implements Serializable {

    private Employee employee;
    private String label;
    private Calendar dayOfMonth;
    private int hoursTotal;
    private List<WorkLog> workLogList = new ArrayList<WorkLog>();

    public CalendarDayUIModel(Calendar dayOfMonth, Employee employee) {
        this.dayOfMonth = dayOfMonth;
        this.label = String.valueOf(dayOfMonth.get(Calendar.DAY_OF_MONTH));
        this.employee = employee;
        
        if (employee != null) {
            loadModelData();
        }
    }

    private void loadModelData() {
        WorkLogService workLogService = TimeTrackingWebApplication.getService(WorkLogService.class);

        List<WorkLog> dbWorkLogList = workLogService.loadWorkLogForDateAndEmployee(employee, dayOfMonth.getTime());
        if (dbWorkLogList != null && dbWorkLogList.size() > 0) {
            for (WorkLog workLog : dbWorkLogList) {
                workLogList.add(workLog);
                hoursTotal += workLog.getHours();
            }
            if (hoursTotal == 0) {
                WorkLog workLog = workLogList.get(0);
            }
        }
    }

    public String getLabel() {
        return label;
    }

    public void setLabel(String label) {
        this.label = label;
    }

    public Calendar getDayOfMonth() {
        return dayOfMonth;
    }

    public void setDayOfMonth(Calendar dayOfMonth) {
        this.dayOfMonth = dayOfMonth;
    }

    public int getHoursTotal() {
        return hoursTotal;
    }

    public void setHoursTotal(int hoursTotal) {
        this.hoursTotal = hoursTotal;
    }

    public List<WorkLog> getWorkLogList() {
        return workLogList;
    }

    public Employee getEmployee() {
        return employee;
    }

    public Calendar getDate() {
        return dayOfMonth;
    }
}
